<extend name="./public/frame.html"/>
<block name="content">
    <blockquote class="layui-elem-quote search">
        <div class="layui-inline">
            <input type="text" name="coupon_name" lay-verify="required|title" required placeholder="优惠券活动名称"  class="layui-input" id="coupon_name">
        </div>
        <div class="layui-inline">
            <input type="text" class="layui-input" placeholder="开始时间" id="start" style="width: 120px;">
        </div>
        <div class="layui-inline">
            <input type="text" class="layui-input" placeholder="结束时间" id="end" style="width: 120px;">
        </div>
        <div class="layui-inline">
            <select id="order_type" name="order_type" class="layui-input" style="width: 120px;">
                <option value="">选择类型</option>
                <?php foreach($orderTypeName as $key=>$value):?>
                <option value="<?php echo $key;?>"><?php echo $value;?></option>
                <?php endforeach;?>
            </select>
        </div>
        <div class="layui-inline">
            <select id="isRecommedName" name="isRecommedName" class="layui-input" style="width: 150px;">
                <option value="">领取途径</option>
                <?php foreach($isRecommedName as $key=>$value):?>
                <option value="<?php echo $key;?>"><?php echo $value;?></option>
                <?php endforeach;?>
            </select>
        </div>
        <div class="layui-inline">
            <button class="layui-btn" data-type="search" id="searchs">搜索</button>
        </div>
        <div class="layui-inline">
            <a class="layui-btn layui-btn-normal newsAdd_btn" href="javascript:;" data-url="{:url('Coupon/add')}" onclick="add_t(this,'添加优惠券活动')">添加优惠券活动</a>
        </div>
        <div class="layui-inline">
            <button style="margin-left:30px" class="layui-btn" id="excel" lay-event="excel">导出EXCEL</button>
        </div>
    </blockquote>
    <table class="layui-table" lay-data="{width:'auto', height:'auto', url:'{$url}', page: true, limit: 10, id:'test'}" lay-filter="test">
        <thead>
        <tr>
            <th lay-data="{checkbox:true, fixed: true}"></th>
            <th lay-data="{field:'coupon_id', width:70,fixed: true, sort: true,align:'center'}">ID</th>
            <th lay-data="{field:'orderTypeName', width:100,align:'center'}">活动类型</th>
            <th lay-data="{field:'coupon_name', width:150,align:'center'}">活动名称</th>
            <th lay-data="{field:'isRecommedName', width:120,align:'center'}">领取途径</th>
            <th lay-data="{field:'moneyTypeName', width:120,align:'center'}">优惠类型</th>
            <th lay-data="{field:'over_money', width:80,align:'center'}">满(元)</th>
            <th lay-data="{field:'money', width:100,align:'center'}">优惠/折扣</th>
            <th lay-data="{field:'start_time', width:110,align:'center'}">开始时间</th>
            <th lay-data="{field:'end_time', width:110,align:'center'}">结束时间</th>
            <th lay-data="{field:'region_name', width:100,align:'center'}">城市</th>
            <th lay-data="{field:'couponNum', width:100,align:'center'}">数量(发放)</th>
            <th lay-data="{field:'use_num', width:80,align:'center'}">已使用</th>
            <th lay-data="{field:'audit_status', width:100,align:'center'}">审核状态</th>
            <th lay-data="{field:'admin_name', width:100,align:'center'}">添加者</th>
            <th lay-data="{fixed:'right', toolbar: '#barDemo', width:300, align:'center'}">操作</th>
        </tr>
        </thead>
    </table>

    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit" style="width: 70px">修改/查看</a>
        {{#  if(d.couponNum == 0){ }}
        {{#  } }}
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="use_coupon" style="width: 60px">查看领取</a>
        {{#  if(d.couponNum == 0){ }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" style="width: 50px">删除</a>
        {{#  } }}
        <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="supply" style="width: 50px">补发</a>
        <?php if($admin_info['admin_type'] == 1){ ?>
        <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="coupon_audit" style="width: 50px">审核</a>
        <?php } ?>
    </script>

    <script>
        layui.use('table', function () {
            var table = layui.table;
            //监听工具条
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                var coupon_id = data.coupon_id;
                if (obj.event === 'del'){
                    layer.confirm('确定删除吗?',function(index){
                        var url = '{:url("/admin/Coupon/del","coupon_id=")}' + coupon_id
                        $.get(url,function(data){
                            if(data.status == 1){
                                layer.msg(data.msg,{'icon':1});
                                window.location.reload();
                            }else{
                                layer.msg(data.msg,{'icon':2});
                            }
                        },'json')
                        layer.close(index);
                    })
                } else if (obj.event === 'use_coupon') {
                    layer.open({
                        type: 2,
                        title: '优惠券使用情况',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '{:url("/admin/admin_activity/couponDetail","coupon_id=")}' + coupon_id //iframe的url
                    });
                }else if (obj.event === 'coupon_audit') {
                    layer.confirm('确定审核通过吗?', function (index) {
                        var url = "{:U('coupon_audit')}";
                        $.post(url, {'coupon_id': coupon_id}, function (data) {
                            console.log(data);
                            if(data.status == 1){
                                layer.msg(data.msg,{'icon':1});
                                window.location.reload();
                            }else{
                                layer.msg(data.msg,{'icon':2});
                            }
                        }, 'json');
                        layer.close(index);
                        window.location.reload();
                    });
                }else if (obj.event === 'edit') {
                    layer.open({
                        type: 2,
                        title: '编辑优惠券活动',
                        shadeClose: true,
                        shade: 0.8,
                        area: ['100%', '100%'],
                        content: '{:url("/admin/coupon/edit","coupon_id=")}' + coupon_id //iframe的url
                    });
                }else if (obj.event === 'supply') {
                    $.ajax({
                        url:'{:url("/admin/coupon/supplyCoupon")}',
                        async:true,
                        dataType:'json',
                        type:'post',
                        data: {'coupon_id':coupon_id},
                        success:function(res){
                            if(res.code==1){
                                layer.open({
                                    type: 2,
                                    title: '补发优惠券',
                                    shadeClose: true,
                                    shade: 0.8,
                                    area: ['90%', '90%'],
                                    content: '{:url("/admin/coupon/supplyCoupon","coupon_id=")}' + coupon_id //iframe的url
                                });
                            }else{
                                layer.msg(res.msg,{icon: 5});
                            }
                        }
                    });
                }
            });
            $(document).on('click', '#excel', function () {
                var headers = {
                    'coupon_id': 'ID',
                    'coupon_name': '活动名称',
                    'money': '金额(元)',
                    'start_time': '开始时间',
                    'end_time': '结束时间',
                    'region_name': '城市',
                    'use_num': '已使用',
                    'admin_name': '添加者',
                    'comment': '描述'
                };
                var header_str = JSON.stringify(headers);
                layer.confirm('请选择导出内容:', {
                    btn: ['全部数据', '当前页', '取消'] //可以无限个按钮
                    , btn3: function (index, layero) {
                        layer.close(index);
                    }
                }, function (index, layero) {
                    //按钮【按钮一】的回调
                    var start = $("#start").val();
                    var end = $("#end").val();
                    var coupon_name = $("#coupon_name").val();
                    var order_type = $("#order_type").val();
                    var isRecommedName = $("#isRecommedName").val();
                    location = "{:url('excel','type=1&headers=')}" + header_str + "&coupon_name=" + coupon_name + "&order_type=" + order_type + "&start_time=" + start + "&end_time=" + end + "&isRecommedName=" + isRecommedName;
                    layer.close(index);
                }, function (index) {
                    location = "{:url('excel','type=2&headers=')}" + header_str+ "&page=" + _cur_page + "&limit=" + _cur_limit;
                    layer.close(index);
                });
            });
            $('.layui-btn').on('click', function () {
                var start = $("#start").val();
                var end = $("#end").val();
                var coupon_name = $("#coupon_name").val();
                var order_type = $("#order_type").val();
                var isRecommedName = $("#isRecommedName").val();
                table.reload('test', {
                    url: '{$url}', where: {
                        "start_time": start,
                        "end_time": end,
                        "coupon_name": coupon_name,
                        "order_type": order_type,
                        "isRecommedName": isRecommedName,
                    }
                });
            });
        });
        layui.use('laydate', function () {
            var laydate = layui.laydate;
            //墨绿主题
            laydate.render({
                elem: '#start' //指定元素
            });
            laydate.render({
                elem: '#end' //指定元素
            });
        });

    </script>
</block>
